package com.mrjy.maintain.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrjy.common.entity.MessageInfo;
import com.mrjy.common.entity.QuestionComment;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.maintain.bo.IMessageInfoBo;

@RequestMapping("/maintain/msgInfo")
@Controller
public class MessageInfoController {

	@Resource
	private IMessageInfoBo messageInfoBo;
	
	/**
	 * 获取消息 
	 * 个人中心-我的消息 
	 * @param session 
	 * @param messageInfo messageInfo.page messageInfo.rows messageInfo.msgModule(消息类型)
	 * @author csshui isUse=true
	 * @return
	 */
	@RequestMapping("/queryMsgList")
	@ResponseBody
	public Object queryMsgList(HttpSession session,MessageInfo messageInfo){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		return messageInfoBo.queryMsgList(userInfo,messageInfo);
	}
	
	/**
	 * TODO 修改更新方法，没有做事务处理
	 * 更新已读标志
	 * 个人中心-更改消息为已读
	 * @param session
	 * @param messageInfo messageInfo.id 消息的id
	 * @return
	 */
	@RequestMapping("/updateReadFlag")
	@ResponseBody
	public Object updateReadFlag(HttpSession session,MessageInfo messageInfo){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		return messageInfoBo.updateMsgFlag(userInfo, messageInfo);
	}
	
	/**
	 * 查询用户已读信息
	 * @param session
	 * @param messageInfo
	 * @return
	 */
	@RequestMapping("/queryMsgIsRead")
	@ResponseBody
	public Object queryMsgIsRead(HttpSession session,MessageInfo messageInfo){
		UserInfo userInfo = (UserInfo)session.getAttribute(GlobalConstant.USER_INFO);
		return messageInfoBo.queryListIsRead(userInfo, messageInfo);
	}
	
	/**
	 * 查询用户未读信息
	 * @param session
	 * @param messageInfo
	 * @return
	 */
	@RequestMapping("/queryMsgNotRead")
	@ResponseBody
	public Object queryMsgNotRead(HttpSession session,MessageInfo messageInfo){
		UserInfo userInfo = (UserInfo)session.getAttribute(GlobalConstant.USER_INFO);
		return messageInfoBo.queryListNotRead(userInfo, messageInfo);
	}
	/**
	 * 查询用户未读条数
	 * @author csshui
	 * @param userId
	 * @return map
	 */
	@RequestMapping("/selectNotReadMsgCountByUserId")
	@ResponseBody
	public Object selectNotReadMsgCountByUserId(@RequestParam("userId")Integer userId){
		return messageInfoBo.selectNotReadMsgCountByUserId(userId); 
	}
	
	/**
	 * 分页查询消息列表
	 * @param info
	 * @param session
	 * @author Javin
	 */
	@RequestMapping("/queryPageMessageByUserId")
	@ResponseBody
	public Object queryPageMessageByUserId(MessageInfo info,HttpSession session){
		UserInfo userInfo = (UserInfo)session.getAttribute(GlobalConstant.USER_INFO);
		Map<String, Object> map = new HashMap<String, Object>();
		if(null == userInfo){
			map.put("err_code", -1);
			map.put("err_msg", "用户数据不存在");
			return map;
		}
		info.setUserId(userInfo.getId());
		return messageInfoBo.queryPageMessageByUserId(info,map);
	}
	/**
	 * 查询消息（评论+通知）
	 * @param userid
	 * @param session
	 * @author huangfei
	 */
	@RequestMapping("/queryMyPageMessageByUserId")
	@ResponseBody
	public Object queryMyPageMessageByUserId(MessageInfo info,HttpSession session){
		UserInfo userInfo = (UserInfo)session.getAttribute(GlobalConstant.USER_INFO);
		Map<String, Object> map = new HashMap<String, Object>();
		if(null == userInfo){
			map.put("err_code", -1);
			map.put("err_msg", "用户数据不存在");
			return map;
		}
		info.setUserId(userInfo.getId());
		return messageInfoBo.queryMyPageMessageByUserId(info,map);
	}
	/**
	 * 查询消息数（回复+评论）
	 * @param userid
	 * @param session
	 * @author huangfei
	 */
	@RequestMapping("/queryMyMessageByUserId")
	@ResponseBody
	public Object queryMyMessageByUserId(MessageInfo info,HttpSession session){
		UserInfo userInfo = (UserInfo)session.getAttribute(GlobalConstant.USER_INFO);
		Map<String, Object> map = new HashMap<String, Object>();
		if(null == userInfo){
			map.put("err_code", -1);
			map.put("err_msg", "用户数据不存在");
			return map;
		}
		info.setUserId(userInfo.getId());
		return messageInfoBo.queryMyMessageByUserId(info,map);
	}
	
	
	/**
	 * 1.6.8
	 * 批量已读 点赞和互动信息（消除家园和顾问的红色消息数字）
	 * @param info
	 * @param session
	 * @return
	 */
	@RequestMapping("/updateMsgStatusBatch")
	@ResponseBody
	public Object updateMsgStatusBatch(QuestionComment comment,HttpSession session){
		return messageInfoBo.txUpdateMsgStatusBatch(comment,session);
	}
}
